package com.avecsys.quotemanager.utils;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.SimpleEmail;

import com.avecsys.quotemanager.constants.ApplicationConstants;
import com.avecsys.quotemanager.exception.SystemConfigurationException;

/**
 * @author Sam Sundar K
 * 
 */
public class EmailSender {

	private static final Log log = LogFactory.getLog(EmailSender.class);

	/**
	 * 
	 * @param toEmailAddressList
	 * @param fromEmailAddress
	 * @param subject
	 * @param messageContent
	 * @param mailType
	 * @throws EmailException
	 * @throws SystemConfigurationException
	 */
	public static void sendMail(List<String> toEmailAddressList,
			String fromEmailAddress, String subject, String messageContent,
			String mailType) throws EmailException,
			SystemConfigurationException {
		log.debug("Sending Mail processing started");
		String smtpHostName = SystemConfig
				.getProperty(ApplicationConstants.SMTP_HOST);
		String smtpPort = SystemConfig
				.getProperty(ApplicationConstants.SMTP_PORT);
		String smtpUserName = SystemConfig
				.getProperty(ApplicationConstants.SMTP_USERNAME);
		String smtpPassword = SystemConfig
				.getProperty(ApplicationConstants.SMTP_PASSWORD);

		Email emailMessage = null;
		if (ApplicationConstants.MAIL_TYPE_HTML.equals(mailType)) {
			emailMessage = new HtmlEmail();
		} else {
			emailMessage = new SimpleEmail();
		}
		for (String toEmail : toEmailAddressList) {
			emailMessage.addTo(toEmail);
		}
		if (fromEmailAddress == null || fromEmailAddress.trim().length() == 0) {
			if (SystemConfig
					.getProperty(ApplicationConstants.APPLICATION_EMAIL_FROM_ADDRESS) == null) {
				log.error("Applicant Email From Address Missing");
			} else {
				emailMessage
						.setFrom(SystemConfig
								.getProperty(ApplicationConstants.APPLICATION_EMAIL_FROM_ADDRESS));
			}
		}
		// Add Mail Bounce address
		emailMessage
				.setBounceAddress(SystemConfig
						.getProperty(ApplicationConstants.APPLICATION_EMAIL_BOUNCE_ADDRESS));
		emailMessage.setSubject(subject);
		if (ApplicationConstants.MAIL_TYPE_HTML.equals(mailType)) {
			((HtmlEmail) emailMessage).setHtmlMsg(messageContent);
		} else {
			emailMessage.setMsg(messageContent);
		}
		if (smtpHostName == null) {
			log.error("Applicant Email From Address Missing");
			throw new SystemConfigurationException();
		} else {
			emailMessage.setHostName(smtpHostName);
		}
		if (smtpPort != null && smtpPort.trim().length() > 0) {
			log.debug("Default SMTP Port Has been overriden by:" + smtpPort);
			emailMessage.setSmtpPort(Integer.parseInt(smtpPort));
		}
		emailMessage.setAuthentication(smtpUserName, smtpPassword);
		emailMessage.send();
		log.info("Email sent successfully to:" + toEmailAddressList.get(0));
	}
}
